<?php
class MapperAutors
{
	public function __construct() {
		mysql_connect(HOST, LOGIN, PASS) or die ('No connect with database');
		mysql_query('SET NAMES utf8');
		mysql_select_db(DB) or die ("error");
	}
	function __destruct() {
	//	mysql_close();
	}
	public function createAutor($name, $pass) {
		$name = Validator::validatorXss($name);
		$name = Validator::validatorSql($name);
		$pass = Validator::validatorXss($pass);
		$pass = Validator::validatorSql($pass);
		$pass = md5($pass);
		$query = "INSERT INTO `users` (name, pass, role) VALUES( '{$name}' , '{$pass}', 2)";
		$r = mysql_query($query) or die(mysql_error());

        if ($r) {
			$idUser = mysql_insert_id();
			$user = $this->getAutor($idUser);
			return $user;
		} else {
			return false;
		}

	}
	public function getAllAutors() {
		$query = "SELECT * FROM users WHERE role = 2";
		$r = mysql_query($query) or die(mysql_error());
		$n = mysql_num_rows($r);
		$autors = array();
		for ($i = 0; $i < $n; $i++) {
			$row = mysql_fetch_assoc($r);
			$autor = new ModelAutors($row);
			$autors[] = $autor;
		}
		return $autors;
	}

    public function getAllBlockAutors()
    {
		$query = "SELECT * FROM users WHERE role = 3";
		$r = mysql_query($query) or die(mysql_error());
		$n = mysql_num_rows($r);
		$autors = array();
		for ($i = 0; $i < $n; $i++) {
			$row = mysql_fetch_assoc($r);
			$autor = new ModelAutors($row);
			$autors[] = $autor;
		}
		return $autors;
	}

	public function getAutor($id) {
		$query = "SELECT * FROM users WHERE id_user = {$id}";
		$r = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_assoc($r);
		$autor = new ModelAutors($row);
		return $autor;
	}
	//когда будем искать пользователя при регистрации нового(чтобы не создавать 2-х с одним именем), пароль не будем передавать.
	public function findAutor($name, $pass = null) {
		$name = Validator::validatorSql($name);
		$name = Validator::validatorXss($name);
		$pass = Validator::validatorSql($pass);
		$pass = Validator::validatorXss($pass);
		$query = "SELECT * FROM `users` WHERE `name` = '{$name}' ";
		$r = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_assoc($r);

        if ($pass == null) {
			if ($row) {
				return true; 
			} else {
				return false;	
            }
		}

		if ($row['pass'] == md5($pass)) {
			$autor = new ModelAutors($row);
			return $autor;
        } else {
               return false;
        }

	}
	public function blockAutor($id) {
		$query = "UPDATE  `users` SET `role` = 3 WHERE  `id_user` = {$id}";
		$r = mysql_query($query) or die(mysql_error());
		return true;
	}
	public function unblockAutor($id) {
		$query = "UPDATE  `users` SET `role` = 2 WHERE  `id_user` = {$id}";
		$r = mysql_query($query) or die(mysql_error());
		return true;
	}
	public function increaseKarma($id) { 
		$query = "UPDATE  `users` SET `karma` = (`karma` + 1) WHERE  `id_user` = {$id}";
		$r = mysql_query($query) or die(mysql_error());
		$queryKarma = "SELECT karma FROM `users` WHERE `id_user` = {$id}";
		$result = mysql_query($queryKarma) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		return $row['karma'];
	}
	public function getKarma($id) {
		$queryKarma = "SELECT karma FROM `users` WHERE `id_user` = {$id}";
		$result = mysql_query($queryKarma) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		return $row['karma'];
	}
	public function decreaseKarma($id) {
		$query = "UPDATE  `users` SET `karma` = `karma`-0.01 WHERE  `id_user` = {$id} ";
		$r = mysql_query($query) or die(mysql_error());
		$queryKarma = "SELECT * FROM `users` WHERE `id_user` = {$id} ";
		$result = mysql_query($queryKarma) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
		return $row['karma'];
	}
	public function setVoice($id_user, $id_autor) {
		$times = time();
		$query = "INSERT INTO voices (id_autor, id_user, time) VALUES( '{$id_autor}' , '{$id_user}', {$times})";
		$r = mysql_query($query) or die(mysql_error());
		return true;
	}
	public function getTimeEndVoice($id) {
		$query = "SELECT  `time` FROM  `voices` WHERE  `id_user` = {$id} ORDER BY  `time` DESC LIMIT 0 , 1";
		$result = mysql_query($query) or die(mysql_error());
		$n = mysql_num_rows($result);
		if (!empty($n)) {
			$row = mysql_fetch_assoc($result);
			return $row['time'];
		} else {
			return null;
		}
	}
}
